package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.CmsMemberReport;
import com.macro.mall.service.CmsMemberReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * @author super
 * @title: CmsMemberReportController
 * @description: TODO
 * @date 2020/10/8 16:37
 */

@Slf4j
@RestController
@RequestMapping("report")
@Api(tags = "report", description = "反馈表(cms_member_report)")
public class CmsMemberReportController {

    @Autowired
    CmsMemberReportService cmsMemberReportService;
    @ResponseBody
    @ApiOperation("查询列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@ApiParam(value = "类型：0->商品评价；1->话题内容；2->用户评论 3->纪委会12345", required = false) @RequestParam(value = "reportType", required = false) String reportType,
                             @ApiParam(value = "用户手机号", required = false) @RequestParam(value = "reportMemberName", required = false) String reportMemberName,
                             @ApiParam(value = "用户真实姓名", required = false) @RequestParam(value = "name", required = false) String name,
                             @ApiParam(value = "状态：0->未处理；1->已处理", required = false) @RequestParam(value = "reportStatus", required = false) String reportStatus,
                             @ApiParam(value = "处理结果：0->无效；1->有效；2->恶意", required = false) @RequestParam(value = "handleStatus", required = false) String handleStatus,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<CmsMemberReport> cmsContents = cmsMemberReportService.find(reportType, reportMemberName, name, reportStatus, handleStatus,pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(cmsContents));
    }


    @ApiOperation("添加")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(Principal principal, @ApiParam(value = "请求报文") @RequestBody CmsMemberReport request) {
        try {
            if(principal==null){
                return CommonResult.unauthorized(null);
            }
            cmsMemberReportService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }
    @ApiOperation("查询内容详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CmsMemberReport> getById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Long id) {
        try {
            CmsMemberReport one = cmsMemberReportService.findOne(id);
            return CommonResult.success(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除内容详情")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Long id) {
        try {
            cmsMemberReportService.deleteById(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("更新内容")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@Valid @ApiParam(value = "请求报文") @RequestBody CmsMemberReport request) {
        try {
            cmsMemberReportService.updateNotNullById(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
